<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
* MODULE NAME   : Bb_vehicle.php
*
* DESCRIPTION   : Event module controller
*
* MODIFICATION HISTORY
*   V1.0   2007-04-21 12:01 PM   - Dennis T. Kaplan     - Created
*
* @package             Bb_vehicle
* @subpackage          bb_vehicle component Class
* @author              Dennis T. Kaplan
* @copyright           Copyright (c) 2006-2007 PzzAzz
* @since               Version 1.0
* @filesource
*/


class Event extends Controller {

    
    function setVars()
    {
		$this->tpl['title'] = 'Billboard Event';
        $this->tpl['head'] = '';
        $this->tpl['sidebar'] = 'Test';
        $this->load->library('profiler');
		$this->uid = $this->session->userdata('uid');
        $this->table = 'bb_event';
	}
    
    function __construct() {
        parent::Controller();
        $this->auth->CheckTaMeR(11,100);
        $this->setVars();
        $this->load->model('master_model', 'mdb');
        $this->load->model('bb_model', 'bbm');
    }

   function browse() {

      $start = $this->uri->segment(4,0);
      
      $limit_per_page = 5;

      $this->bbm->findWhat('Title, Postcode, Starts, Ends, Active'); 
      $event_list = $this->bbm->findAll($start, $limit_per_page); 
      //$event['rowcount'] = count($event['list']);

      $this->load->library('table');
      foreach ($event_list as $row) {
          $this->table->add_row($row);
      }
      $this->table->set_heading(array_keys($row));
      $this->tpl['body'] = $this->table->generate();
      $this->table->clear();      
      $this->load->library('pagination');

      $config['base_url']     = site_url('billboard/event/browse');   // or just /event/
      $config['total_rows']   = $this->bbm->table_record_count;
      $config['per_page']     = $limit_per_page;

      $this->pagination->initialize($config);

      $this->tpl['body'] .= $this->pagination->create_links();

      $this->load->view('blank', $this->tpl);
   }

   
    function Form()
    {
        $this->tpl['tpljs'][]  = 'jquery';
        $this->tpl['tplcss'][]  = 'calendar';
        $this->tpl['tpljs'][]  = 'calendar';
        $this->tpl['tpljs'][]  = 'jquery.bgiframe.min';
        $this->tpl['tpljs'][]  = 'jquery.dimensions';
        $this->tpl['tplcss'][]  = 'jquery.autocomplete';
        $this->tpl['tpljs'][]  = 'jquery.autocomplete';
        $this->tpl['tpljs'][]  = 'postcode';
        
        $action = $this->uri->segment(4);
        $id = $this->uri->segment(5);
        
        if( ! isset($this->dbdata)) {
            if($id < 1) $id = FALSE;
            $this->dbdata = $this->mdb->initdb($id);
        }
        extract($this->dbdata);

        $attributes = array('name' => 'event');
        $this->load->helper('form');
        $form['Title'] = form_input(array(
                                        'name'        => 'Title',
                                        'id'          => 'Title',
                                        'value'       => $Title,
                                        'maxlength'   => '30',
                                        'size'        => '30',
                                        'style'       => ''
                                        )
                                    );

        $form['Address'] = form_input(array(
                                        'name'        => 'Address',
                                        'id'          => 'Address',
                                        'value'       => $Address,
                                        'maxlength'   => '200',
                                        'size'        => '40',
                                        'style'       => ''
                                        )
                                    );

        $form['Postcode or City'] =  form_input(array(
                                    'name'        => 'Postcode',
                                    'id'          => 'Postcode',
                                    'value'       => $Postcode,
                                    'size'        => '10',
                                    'onFocus'     => 'clearValue(this)'
                                    )
                                    ).'<span id="postresult"></span>';
        
        $form[2]['Start Date'] = form_input(array(
                                        'name'        => 'Starts',
                                        'id'          => 'Starts',
                                        'value'       => $Starts,
                                        'maxlength'   => '10',
                                        'size'        => '10',
                                        'style'       => 'width: 100px',
                                        'class'       => 'calendarSelectDate'
                                        )
                                    );

        $form[2]['End Date'] = form_input(array(
                                        'name'        => 'Ends',
                                        'id'          => 'Ends',
                                        'value'       => $Ends,
                                        'maxlength'   => '10',
                                        'size'        => '10',
                                        'style'       => 'width: 100px',
                                        'class'       => 'calendarSelectDate'

                                        )
                                    );
        $form['Website'] = form_input(array(
                                        'name'        => 'Website',
                                        'id'          => 'Website',
                                        'value'       => $Website,
                                        'maxlength'   => '200',
                                        'size'        => '40',
                                        'style'       => ''
                                        )
                                    );
        
        $form['Info'] = form_textarea(array(
                                        'name'        => 'Info',
                                        'id'          => 'Info',
                                        'value'       => $Info,
                                        'rows'        => '15',
                                        'cols'        => '60',
                                        'style'       => ''
                                        )
                                    );
        $form[''] =          form_submit(array('class' => 'submit', 
                                        'name'        => 'submit_event',
                                        'value'       => 'Submit',
                                        'style'       => 'margin:10px'
                                        )
                                    );

        $this->load->library('table');
        $this->tpl['body'] =  form_open("billboard/event/$action", $attributes);
        $this->tpl['body'] .= $this->table->form_table($form);
        $this->tpl['body'] .= form_hidden('id', $id);
        $this->tpl['body'] .= '</form>';
        $this->tpl['body'] .= '<p><span id="resulttag"></span></p>';
        $this->load->view('blank', $this->tpl);
    } 
   
   
   function add() {

      $this->load->helper('url');

      // ///////////////////////////////////////////////////////////////////////
      // How are we being invoked. The user is either requesting a form or is
      // ...submitting it.
      // ///////////////////////////////////////////////////////////////////////
      $submit = $this->input->post('Submit');

      if ( $submit != false ) {
         // ////////////////////////////////////////////////////////////////////
         // User is submitting data
         // Store the values from the form onto the db
         // ////////////////////////////////////////////////////////////////////
         $this->load->model('bbm');

         /*
		// XXS Filtering enforced for user input
		$data['id']		= $this->input->post('id', TRUE);
		$data['Title']		= $this->input->post('Title', TRUE);
		$data['postcode_id']		= $this->input->post('postcode_id', TRUE);
		$data['Starts']		= $this->input->post('Starts', TRUE);
		$data['Ends']		= $this->input->post('Ends', TRUE);
		$data['Map']		= $this->input->post('Map', TRUE);
		$data['Website']		= $this->input->post('Website', TRUE);
		$data['Info']		= $this->input->post('Info', TRUE);
		$data['Active']		= $this->input->post('Active', TRUE);
		$data['user_id']		= $this->input->post('user_id', TRUE);
		$data['updated']		= $this->input->post('updated', TRUE);
		$data['added']		= $this->input->post('added', TRUE);

         */
         $data = $this->_get_form_values();

         $this->bbm->add($data);

         // $this->load->helper('url');
         redirect('/event/', 'location');

      }
      else {
         // We have to show the user the input form
         /*
		$data['id']		= '';
		$data['Title']		= '';
		$data['postcode_id']		= '';
		$data['Starts']		= '';
		$data['Ends']		= '';
		$data['Map']		= '';
		$data['Website']		= '';
		$data['Info']		= '';
		$data['Active']		= '';
		$data['user_id']		= '';
		$data['updated']		= '';
		$data['added']		= '';

         */
         $data = $this->_clear_form();
         $data['action']       = 'add';

		// Retrieve the places_postcode lookup values.
		$this->load->model('places_postcodemodel');
		$data['places_postcodelist'] = $this->places_postcodemodel->findAll();

		// Retrieve the contact_user lookup values.
		$this->load->model('contact_usermodel');
		$data['contact_userlist'] = $this->contact_usermodel->findAll();



         // Call upon the layout library to draw the input screen
         $this->load->library('layout');
         $this->layout->render_page('/event/eventdetails', $data);
         // NOTE: If you don't want to use the layout library, use the line below.
         // $this->load->view('/event/eventdetails', $data);


      }
   }

   // //////////////////////////////////////////////////////////////////////////
   // Function: modify()
   //
   // Description: Controller function to process user modify requests
   //
   // //////////////////////////////////////////////////////////////////////////
   function modify() {

      $this->load->helper('url');

      // ///////////////////////////////////////////////////////////////////////
      // How are we being invoked
      // ///////////////////////////////////////////////////////////////////////
      $submit = $this->input->post('Submit');

      if ( $submit != false ) {
         // ////////////////////////////////////////////////////////////////////
         // User is submitting data
         // Store the values from the form onto the db
         // ////////////////////////////////////////////////////////////////////
         $this->load->model('bbm');

         // $data['action']          = 'modify';
         /*
		// XXS Filtering enforced for user input
		$data['id']		= $this->input->post('id', TRUE);
		$data['Title']		= $this->input->post('Title', TRUE);
		$data['postcode_id']		= $this->input->post('postcode_id', TRUE);
		$data['Starts']		= $this->input->post('Starts', TRUE);
		$data['Ends']		= $this->input->post('Ends', TRUE);
		$data['Map']		= $this->input->post('Map', TRUE);
		$data['Website']		= $this->input->post('Website', TRUE);
		$data['Info']		= $this->input->post('Info', TRUE);
		$data['Active']		= $this->input->post('Active', TRUE);
		$data['user_id']		= $this->input->post('user_id', TRUE);
		$data['updated']		= $this->input->post('updated', TRUE);
		$data['added']		= $this->input->post('added', TRUE);

         */
         $data = $this->_get_form_values();

         $this->bbm->modify($data['id'], $data);

         redirect('/event/', 'location');

      }
      else {
         // We have to show the user the input form

         $idField = $this->uri->segment(3);

         $this->load->model('bbm');
         $data = $this->bbm->retrieve_by_pkey($idField);
         $data['action'] = 'modify';

		// Retrieve the places_postcode lookup values.
		$this->load->model('places_postcodemodel');
		$data['places_postcodelist'] = $this->places_postcodemodel->findAll();

		// Retrieve the contact_user lookup values.
		$this->load->model('contact_usermodel');
		$data['contact_userlist'] = $this->contact_usermodel->findAll();



         $this->load->library('layout');
         $this->layout->render_page('/event/eventdetails', $data);
         // NOTE: If you don't want to use the layout library, use the line below.
         // $this->load->view('/event/eventdetails', $data);


      }
   }


   // //////////////////////////////////////////////////////////////////////////
   // Function: delete()
   //
   // Description: Controller function to process user delete requests
   //
   // //////////////////////////////////////////////////////////////////////////
   function delete() {
      $idField = $this->uri->segment(3);

      $this->load->model('bbm');
      $the_results = $this->bbm->delete_by_pkey($idField);

      $this->load->helper('url');
      redirect('/event/', 'location');

   }

   function _clear_form() {

      // ///////////////////////////////////////////////////////////////////////
      // NOTE: Set default values for the form here if you wish.
      // ///////////////////////////////////////////////////////////////////////
		$data['id']		= '';
		$data['Title']		= '';
		$data['postcode_id']		= '';
		$data['Starts']		= '';
		$data['Ends']		= '';
		$data['Map']		= '';
		$data['Website']		= '';
		$data['Info']		= '';
		$data['Active']		= '';
		$data['user_id']		= '';
		$data['updated']		= '';
		$data['added']		= '';

      return $data;

   }

   function _get_form_values() {
      // ///////////////////////////////////////////////////////////////////////
      // NOTE: Perform customisation on the retrieved form values here if you wish.
      // ///////////////////////////////////////////////////////////////////////
		// XXS Filtering enforced for user input
		$data['id']		= $this->input->post('id', TRUE);
		$data['Title']		= $this->input->post('Title', TRUE);
		$data['postcode_id']		= $this->input->post('postcode_id', TRUE);
		$data['Starts']		= $this->input->post('Starts', TRUE);
		$data['Ends']		= $this->input->post('Ends', TRUE);
		$data['Map']		= $this->input->post('Map', TRUE);
		$data['Website']		= $this->input->post('Website', TRUE);
		$data['Info']		= $this->input->post('Info', TRUE);
		$data['Active']		= $this->input->post('Active', TRUE);
		$data['user_id']		= $this->input->post('user_id', TRUE);
		$data['updated']		= $this->input->post('updated', TRUE);
		$data['added']		= $this->input->post('added', TRUE);

      return $data;

   }

}
?>
